import {ref} from 'vue'
import {defineStore} from 'pinia'

export const useInventoryStore = defineStore('inventory', () => {
    const inventoryInfo = ref([
        {
            id: 0,
            name: '可乐',//商品名称
            //库存数
            num: 100,
        },
    ])
//增加
    const increment = (item) => {
        inventoryInfo.value.push(item)
        sessionStorage.setItem('inventoryInfo', JSON.stringify(inventoryInfo.value))
    }
    //编辑
    const edit = (item) => {
        inventoryInfo.value.map((v, i) => {
            if (v.id === item.id) {
                inventoryInfo.value[i] = item
            }
        })
        sessionStorage.setItem('inventoryInfo', JSON.stringify(inventoryInfo.value))
    }
//删除
    const del = (id) => {
        inventoryInfo.value.map((v, i) => {
            if (v.id === id) {
                inventoryInfo.value.splice(i, 1)
            }
        })
        sessionStorage.setItem('inventoryInfo', JSON.stringify(inventoryInfo.value))
    }


    return {inventoryInfo, increment, edit, del}
})
